package com.ocom.payconfig.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ocom.common.entity.payconfig.EntityMealMenu;
import com.ocom.common.request.payconfig.mealorder.GetOrderDishesInfoDao;
import com.ocom.common.vo.payconfig.MealMenuVo;
import com.ocom.payconfig.po.MealMenuListBVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface IDaoMealMenu extends BaseMapper<EntityMealMenu> {

	@Select({"<script>",
			"select \n" +
					"M.*,D.dishes_class_id,D.dishes_name \n" +
					"from meal_menu M \n" +
					"left join dishes_info D on M.dishes_id = D.dishes_id  " +
					"where M.com_id = #{comId} and M.merch_id = #{merchId} "+
					"and M.mealplan_id = #{mealplanId} "+
					"<when test=\"days !=null and  days >'0'\" > "+
					"and  M.days = #{days}  "+
					" </when> "+
					"<when test=\"mealNo !=null and  mealNo >'0'\" > "+
					"and  M.meal_no = #{mealNo}  "+
					" </when> "+
					" order by M.days,M.meal_no,M.sort  " +
					"</script>"})
	public List<EntityMealMenu> mealMenuList(@Param("comId")Long comId,
											 @Param("merchId")Integer merchId,
											 @Param("mealplanId")Integer mealplanId,
											 @Param("days")Integer days ,
											 @Param("mealNo")Integer mealNo );


	/**
	 * select  m.dishes_id,m.sort,m.price_mode
	 * ,m.price as  dishesPrice
	 * ,d.photo_url,d.dishes_name,d.dishes_class_Id ,d.remark
	 * ,p.price as dishesPriceNew
	 * from meal_menu m
	 * left join dishes_info d on m.com_id=d.com_id and m.dishes_id=d.dishes_id
	 * left join dishes_price p on m.com_id=p.com_id and m.dishes_id=p.dishes_id
	 * where m.com_Id =  #{comId}  and mealplan_Id=#{mealplanId}  and m.days=#{days}  and m.meal_no=#{mealNo}
	 */

	@Select({"<script>",
			"select  \n" +
					"m.dishes_id,m.sort,m.price_mode, \n" +
					"m.price as  dishesPriceNew, \n" +
					"d.photo_url,d.dishes_name,d.dishes_class_Id ,d.remark, \n" +
					"0 as dishesPrice,m.limited_quantity,m.point_id  \n" +
					"from meal_menu m  \n" +
					"left join dishes_info d on m.com_id=d.com_id and m.dishes_id=d.dishes_id  " +
//					"left join dishes_price p on m.com_id=p.com_id and m.dishes_id=p.dishes_id  " +
					"where  m.com_Id =  #{comId}  and mealplan_Id=#{mealplanId}   "+
					"and m.days=#{days}  and m.meal_no=#{mealNo} "+
					"order by m.days,m.meal_no,m.sort  " +
					"</script>"})
	public List<MealMenuVo> getMealList(@Param("comId")Long comId,
										 @Param("mealplanId")Integer mealplanId,
										 @Param("days")Integer days ,
										 @Param("mealNo")Integer mealNo );

	@Select({"<script>",
			"select  " +
					" m.mealplan_id," +
					" m.dishes_id," +
					" m.meal_no," +
					" m.sort," +
					" GROUP_CONCAT(distinct concat( m.price_mode,',',p.price,',',m.price,',',m.limited_quantity)  ORDER BY m.price_mode SEPARATOR ';')  as priceInfo," +
					" d.photo_url," +
					" d.dishes_name," +
					" d.dishes_class_Id ," +
					" d.remark ," +
					" d.quota_min ," +
					" d.quota_max," +
					" d.sort as dishes_sort" +
					" from meal_menu m " +
					" left join dishes_info d on m.com_id=d.com_id and m.dishes_id=d.dishes_id" +
					" left join dishes_price p on m.com_id=p.com_id and m.dishes_id=p.dishes_id and  m.price_mode=p.price_mode " +
					" and (p.point_id= (select  max(point_id) from  dishes_price p2 where p2.com_id=m.com_id and p2.dishes_id=m.dishes_id and  p2.price_mode=m.price_mode  ))" +
					" where m.com_Id = #{comId} " +
					" and mealplan_Id=#{mealplanId} " +
					" and m.days=#{days} " +
					" <when test=\"mealNo !=null and  mealNo &gt; 0\" > "+
					" and m.meal_no=#{mealNo}" +
					"</when>" +
					" group by m.dishes_id " +
					" order by d.dishes_class_Id ,m.sort" +
					"</script>"})
	public List<GetOrderDishesInfoDao> getDishesInfo(@Param("comId")Long comId,
													 @Param("mealplanId")Integer mealplanId,
													 @Param("days")Integer days ,
													 @Param("mealNo")Integer mealNo );


	@Select("SELECT  M.mealplan_id ,M.days,M.meal_no ,M.dishes_id, D.dishes_name ,D.remark,D.sort"
					+" FROM meal_menu M left join dishes_info D on M.dishes_id = D.dishes_id"
					+" WHERE M.com_id = #{comId} and M.merch_id = #{merchId} and M.mealplan_id = #{mealplanId}"
					+" ORDER by M.days , M.meal_No , D.dishes_name, D.sort"
					)
	List<MealMenuListBVO> getMealMenuListB(@Param("comId")Long comId, @Param("merchId")Integer merchId, @Param("mealplanId")Integer mealplanId);

}
